package Csp;

import java.util.ArrayList;
import java.util.List;

import Csp.Model.Csp;
import Csp.Model.Variable;
import Csp.Reader.CspReader;
import Csp.Solvers.AC3;
import Csp.Solvers.Backtracking;
import Csp.Solvers.CommonHeuristics;
import Csp.Solvers.ForwardChecking;
import Csp.Solvers.IHeuristics;
import Csp.Solvers.ISolver;
import Csp.Solvers.TrivialHeuristics; 

public class Main {

	public static void main(String[] args) {
		
		if(args.length == 0 || args[0].isEmpty()){
			System.out.println("Missing csp problem");
			return;
		}
			
		String path = args[0];
		int timeout = args.length == 1 || args[1].isEmpty() ? 60 : Integer.valueOf(args[1]);
		
		System.out.println(String.format("\n\nCsp file is %s", path));
		System.out.println(String.format("Timeout is %s seconds\n\n", timeout));
		
		Csp csp = CspReader.ReadFromFile(path);
	
		IHeuristics trivial = new TrivialHeuristics();
		IHeuristics common = new CommonHeuristics();
		
		List<ISolver> solvers = new ArrayList<ISolver>();
		solvers.add(new Backtracking(trivial, timeout));
		solvers.add(new ForwardChecking(trivial, timeout));
		solvers.add(new ForwardChecking(common, timeout));
		solvers.add(new AC3(trivial, timeout));
		solvers.add(new AC3(common, timeout));

		for (ISolver solver : solvers) {
			System.out.println(String.format("SOLVER: %s", solver.getName()));
			System.out.print("	Result: ");
			List<Variable> result = solver.solve(csp);
			if(result != null){
				for (Variable variable : result){
					System.out.print(String.format("V%s-%s ", variable.getId(), variable.getDomain().iterator().next()));
				}
			}
			else
				System.out.print("unsolvable");
			
			System.out.println();
			System.out.println("	Total time: "+solver.getTime()+" ms");
			System.out.println("	Total nodes: "+solver.getNodes());
			System.out.println();
		}
	}
	
}
